<template>
  <a-drawer
    :visible="visible"
    @close="onClose"
    title="字典列表"
    width="1000px"
  >
    <a-card :bordered="false" class="content">
      <div class="table-operator">
        <a-button
          @click="$refs.dictLabelAddOrUpdate.init('','add',type)"
          icon="plus"
          type="primary"
          v-action="'sys:dict:item:save'">新建
        </a-button>
      </div>
      <s-table
        :columns="columns"
        :data="loadData"
        :rowKey="record => record.id"
        bordered
        ref="table"
      >
        <span slot="action" slot-scope="text, record">
          <span v-action="'sys:dict:item:update'">
            <a @click="edit(record)">修改</a>
            <a-divider type="vertical"/>
          </span>
          <span v-action="'sys:dict:item:del'">
            <a-popconfirm @confirm="del(record)" cancelText="否" okText="是" title="是否删除字典">
              <a-icon slot="icon" style="color: red" type="question-circle-o"/>
              <a href="javascript:" style="color: red">删除</a>
            </a-popconfirm>
          </span>
        </span>
      </s-table>
      <dict-label-add-or-update
        @ok="queryPage"
        ref="dictLabelAddOrUpdate"
      />
    </a-card>
  </a-drawer>
</template>

<script>
import { delObj, fetchList } from '@api/sys/label'
import DictLabelAddOrUpdate from './dictLabelAddOrUpdate'
import { TableMixin } from '@/mixins/TableMixin'

export default {
  name: 'DictItemDrawer',
  mixins: [TableMixin],
  components: {
    DictLabelAddOrUpdate
  },
  data () {
    return {
      visible: false,
      loading: false,
      columns: [
        {
          title: '序号',
          width: '80px',
          align: 'center',
          customRender: (text, record, index) => {
            return (
              <div>
                {index + 1}
              </div>
            )
          }
        },
        {
          title: '标签',
          align: 'center',
          dataIndex: 'label'
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'type'
        },
        {
          title: '操作',
          width: '180px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      tableData: [],
      queryParam: {
        name: '',
        type: ''
      },
      type: '',
      loadData: parameter => {
        return fetchList(
          Object.assign(parameter, this.queryParam), this.type
        ).then(res => {
          return res.data
        })
      }
    }
  },
  methods: {
    init (record) {
      this.visible = true
      this.type = record.type
      console.log(record, 'record')
      this.queryPage()
    },
    edit (record) {
      this.$refs.dictLabelAddOrUpdate.init(record.id, 'edit', this.type)
    },
    del (record) {
      delObj(record.id).then(() => {
        this.$message.info('删除成功')
        this.queryPage()
      })
    },
    onClose () {
      this.visible = false
    }
  }
}
</script>

<style scoped>

</style>
